Repository navigation
Conversation
Grid.moments read self.points.shape[1], so a grid whose points have shape (N,), such as every OneDGrid, raised IndexError. Treat those points as N points in one dimension, like shape (N, 1).
|
This seems straightforward and OK. I would probably have done the simpler option of merely returning an error if the moments weren't 3D, but this is OK. What do you think @marco-2023 |
There was a problem hiding this comment.
🟡 Changes recommended
The unconditional reshape in src/grid/basegrid.py breaks Cartesian moments for supported empty 2D grids, which previously returned zeros.
1 open issue
What changed in this PR
Extends Grid.moments to accept flat (N,) point arrays used by OneDGrid, building on the earlier fix for one-dimensional Cartesian orders.
Changes:
- Normalizes points locally for dimension checks, order generation, and centering.
- Adds regression coverage comparing flat
GridandOneDGridmoments with(N, 1)grid moments.
| File | Description |
|---|---|
src/grid/tests/test_grid.py |
Tests Cartesian moment equivalence for flat and column-shaped points. |
src/grid/basegrid.py |
Normalizes point arrays before computing moments. |
Review effort: Balanced
|
|
||
| """ | ||
| # Points of shape (N,), as in OneDGrid, are N points in one dimension | ||
| points = self.points.reshape(len(self.points), -1) |
There was a problem hiding this comment.
Good catch, thanks. Fixed in 96d54eb: 2D points are used as they are, and only points of shape (N,) get the extra axis. Added test_moments_empty_localgrid for every grid dimension; it fails with the previous commit ("cannot reshape array of size 0") and passes now.
Reshaping every points array with reshape(len(points), -1) fails for an empty local grid of shape (0, 3), whose moments used to be zero. Only add the singleton axis for points of shape (N,).

Summary
Grid.momentsraisesIndexErroron a grid whose points have shape(N,). That includes everyOneDGrid(GaussLegendre,Trapezoidal, ...), even though the constructor accepts 1D point arrays:The same points as shape
(N, 1)work since #335 and give[2, 0, 2/3].Grid.moments: usesself.points.reshape(len(self.points), -1)locally, so(N,)points are treated asNpoints in one dimension, like(N, 1). The dimension check, the centering and the order generation all use it. Grids with 2D points are unchanged.Tests
TestGrid1D.test_cartesian_moments_flat_pointschecks that aGridand aOneDGridwith(N,)points give the same moments as the(N, 1)grid. It fails onmasterwith theIndexErrorabove and passes with this change.test_grid.pyandtest_atomgrid.py: 94 passed. ruff and black are clean on the changed lines.